<?php
namespace Admin\Controller;
use Think\Controller;
class UsersController extends Controller {
    public function index(){
      $admin = M('admin');
      $role = M('role');
      $rom = $admin -> select();
      foreach ($rom as $key => $value) {
        $rom[$key]['rolename'] = $role->where('id='.$value['sid'])->find();
      }
      $this->assign('rom',$rom);
      //dump($rom);
        $this->display();
    }
 
   public function role(){
    $role = M('role');
    $rom = $role -> select();
    foreach ($rom as $key => $value) {
      if($value['parentid'] == 0){
          $rom[$key]['name']['rolename'] = "最高级别";
      }
      $rom[$key]['name'] = $role -> where('id='.$value['parentid']) -> find();
    }
    $this->assign('rom',$rom);
   	$this->display();
   }

   public function department(){
    $department = M('department');
     $rom=$department->select();
      foreach ($rom as $key => $value) {
      $rom[$key]['name'] = $department -> where('id='.$value['parentid']) -> find();
    }
    if($_GET['id']){
      $vo = $department -> where('id='.$_GET['id']) -> find();
    }
    if(IS_POST){
     // dump($_POST);exit;
      $data = $_POST;
      if($_POST['id'] != 0){
        $rs = $department -> where('id='.$_POST['id']) -> save($data);
      }else{
        $rs = $department -> add($data);
      }
      if($rs){
      $this->success('操作成功！',U('Users/department'));
     }else{
      $this->error('操作失败！');
     }
    }
    $this->assign('rom',$rom);
   	$this->display();
   }

   public function add(){
    $admin=M("admin");
   	if(IS_POST){
      $data=$_POST;
      $data['password'] = md5($_POST['password']);
     if($_POST['id'] != 0){
         $rs=$admin -> where('id='.$_POST['id']) -> save();
     }else{
        $rs = $admin->add($data);
     }
     if($rs){
      $this->success('操作成功！',U('Users/index'));
     }else{
      $this->error('操作失败！');
     }

   	}
    if($_GET['id']){
      $id = $_GET['id'];
      $rom=$admin->where('id='.$_GET['id'])->find();
    }
    $this->assign('rom',$rom);
    $this->assign('id',$id);
   	$this->display();
   }

   public function role_add(){
    $role = M('role');
    $rom=$role->select();
    if($_GET['id']){
      $vo = $role -> where('id='.$_GET['id']) -> find();
    }
    if(IS_POST){
      $data = $_POST;
      $data['time'] = time();
      if($_POST['id']){
        $rs = $role -> where('id='.$_POST['id']) -> save($data);
      }else{
        $rs = $role -> add($data);
      }
      if($rs){
      $this->success('操作成功！',U('Users/role'));
     }else{
      $this->error('操作失败！');
     }
    }
    // dump($vo);
    $this->assign('vo',$vo);
    $this->assign('rom',$rom);
    $this->display();
   }

   public function department_add(){
    $department = M('department');
    $rom=$department->select();
    if($_GET['id']){
      $vo = $department -> where('id='.$_GET['id']) -> find();
    }
    if(IS_POST){
      $data = $_POST;
      if($_POST['id'] != 0){
        $rs = $department -> where('id='.$_POST['id']) -> save($data);
      }else{
        $rs = $department -> add($data);
      }
      if($rs){
      $this->success('操作成功！',U('Users/department'));
     }else{
      $this->error('操作失败！');
     }
    }
    // dump($vo);
    $this->assign('vo',$vo);
    $this->assign('rom',$rom);
    $this->display();
   }

   public function del(){
    $id = $_GET['id'];
    $table = $_GET['table'];
    $rs = M($table) -> where('id='.$id) -> delete();
    if($rs){
      $this->success('删除成功!');
    }else{
      $this->error('删除失败!');
    }
   }
}